import Vue from 'vue'
import VueRouter from 'vue-router'
import {Message} from "element-ui";
import Home from '../views/Home.vue'
import Login from '../views/login/Login'
import Goods from "../views/goods/Goods";
import GoodsHome from "../views/goods/component/GoodsHome";
import GoodsList from "../views/goods/component/GoodsList";
import GoodsEdit from "../components/goodList/GoodsEdit";
import GoodsPrice from "../views/goods/component/GoodsPrice";
import GoodsType from "../views/goods/component/GoodsType";
import Stock from "../views/stock/Stock";
import StockList from "../views/stock/components/StockList";
import StockHome from "../views/stock/components/StockHome";
import Order from "../views/order/Order";
import OrderList from "../views/order/components/OrderList";
import System from "../views/system/System";
import UserList from "../views/system/components/UserList";
import RoleList from "../views/system/components/RoleList";
import StockUnit from "../views/stock/components/StockUnit";

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name:'default',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {name: "登录"}
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/goods',
        name: 'Goods',
        component: Goods,
        redirect: '/goods/goodsHome',
        meta: {name: "商品和库存"},
        children: [
          {
            path: '/goods/goodsHome',
            name: 'GoodsHome',
            component: GoodsHome,
            meta: {name: "商品首页"}
          },
          {
            path: '/goods/goodsList',
            name: 'GoodsList',
            component: GoodsList,
            meta: {name: "商品列表"},
          },
          {
            path: '/goods/goodsPrice',
            name: 'GoodsPrice',
            component: GoodsPrice,
            meta: {name: "商品列表"},
          },
          {
            path: '/goods/goodsType',
            name: 'GoodsType',
            component: GoodsType,
            meta: {name: "种类列表"},
          },
        ]
      },
      {
        path: '/stock',
        name: 'Stock',
        component: Stock,
        redirect: '/stock/stockList',
        meta: {name: "库存"},
        children:[
          {
            path: '/stock/stockList',
            name: 'StockList',
            component: StockList,
            meta: {name: "库存列表"},
          },
          {
            path: '/stock/stockUnit',
            name: 'StockUnit',
            component: StockUnit,
            meta: {name: "库存单位"},
          },
          // {
          //   path: '/stock/stockHome',
          //   name: 'StockHome',
          //   component: StockHome,
          //   meta: {name: "库存首页"},
          // },
        ]
      },
      {
        path: '/order',
        name: 'Order',
        component: Order,
        redirect: '/order/orderList',
        meta: {name: "订单"},
        children: [
          {
            path: '/order/orderList',
            name: 'OrderList',
            component: OrderList,
            meta: {name: "订单列表"},
          }
        ],
      },
      {
        path: '/system',
        name: 'System',
        component: System,
        redirect: '/system/userList',
        meta: {name: "系统设置"},
        children: [
          {
            path: '/system/userList',
            name: 'UserList',
            component: UserList,
            meta: {name: "用户列表"},
          },
          {
            path: '/system/roleList',
            name: 'RoleList',
            component: RoleList,
            meta: {name: "角色列表"},
          },
        ]
      },
    ]
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

/*
挂载‘路由导航守卫’
to: 将要访问的路径
from: 从哪个路径跳转而来
next: 函数--> 放行
  next() 放行
  next('/login') 强制跳转到指定路径
*/
router.beforeEach((to, from, next) => {
  // 需要放行的路径
  const letGo = [
      '/',
      '/login',
  ];
  // 放行
  if (letGo.indexOf( to.path ) > 0) return next();
  // 获取token, 如果没有登录则跳转到登录页
  const token = sessionStorage.getItem("Authorization");
  if (!token){
    Message({
      message: "你还没登录，请先登录!",
      type: "warning",
      showClose: true
    });
    return next('/login');
  }
  // 放行
  next();
});

export default router
